Report irrefutable match patterns that make later cases unreachable - #21945
Open
istoolsfox wants to merge 1 commit into
Open
Report irrefutable match patterns that make later cases unreachable#21945istoolsfox wants to merge 1 commit into
istoolsfox wants to merge 1 commit into
Conversation
CPython rejects at compile time a match statement whose non-final case
is an unguarded capture or wildcard, or that contains an or-pattern
with an irrefutable alternative in a non-final position ('name capture
'y' makes remaining patterns unreachable'), since such patterns make
the remaining cases unreachable. mypy checked such files clean.
Mirror that check in the match statement checker: report captures and
wildcards in non-final cases (unless guarded) and in non-final or-pattern
alternatives at any nesting depth. Captures inside composite patterns
(sequence, mapping, class) stay allowed, matching CPython. Unlike
CPython, which stops at the first occurrence, all of them are reported.
Fixes python#21925
Contributor
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #21925.
CPython rejects at compile time a match statement whose non-final case is an unguarded capture or wildcard pattern, or that contains an or-pattern with an irrefutable alternative in a non-final position (
SyntaxError: name capture 'y' makes remaining patterns unreachable/wildcard makes remaining patterns unreachable), since such patterns make the remaining cases unreachable. mypy currently checks such files clean — a file that cannot even be imported passes type checking.What
Mirror CPython's irrefutability check (PEP 634) in the match statement checker:
[x],Cls(x),{k: x}) stay allowed, matching CPython — only the positions above are inspected.Differences from CPython:
SyntaxError;Implementation notes
case y as z:is handled naturally: the parser representsyas an inner capture pattern, which the recursion inspects with the outer allow flag — matching CPython's own AST (MatchAs(pattern=MatchAs(name='y'), name='z')).sub_patterns()tomypy/patterns.pyfor enumerating composite sub-patterns.Verification
check-python310.testcovering the full rule matrix (capture/wildcard non-final, guard, final case, or-pattern positions, nested or-patterns, composite captures allowed,x as y, multiple violations).mypy --config-file mypy_self_check.iniclean;ruff checkandruff formatclean on touched files (modulo two pre-existing format drifts on master).